package com.openapi.service;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import com.openapi.data.Session;

import javax.lang.model.element.VariableElement;
import java.util.function.BiConsumer;
import java.util.List;
import java.util.Map.Entry;

public class SessionManager {

	private static volatile SessionManager instance = null;
	// 终端手机号和Session的对应关系
	private Map<String, Session> devNosMap;

	public static SessionManager getInstance() {
		if (instance == null) {
			synchronized (SessionManager.class) {
				if (instance == null) {
					instance = new SessionManager();
				}
			}
		}
		return instance;
	}

	public SessionManager() {
		this.devNosMap = new ConcurrentHashMap<>();
	}
	public Session findBySessionId(String channelId) {
		for (Session item : devNosMap.values()) {
			if(item.getId().equals(channelId)){
				return item;
			}
		}
		return null;
	}

	public Session findByTerminalPhone(String terminalPhone) {
		return 	devNosMap.get(terminalPhone);
	}

	public synchronized void put(String key, Session value) {
		devNosMap.put(key,value);
	}

	public synchronized Session removeBySessionId(String channelId) {
		Session session = findBySessionId(channelId);
		if (session == null)return null;
		return devNosMap.remove(session.getDevNo());
	}

	public int devNoSize() {
		return devNosMap.keySet().size();
	}


}